<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />	
	<title>Order Detail</title>	
	<link href="<?php bloginfo('template_url');?>/admins/css/style.css" rel="stylesheet" type="text/css" />
	<link href="<?php bloginfo('template_url');?>/admins/css/tabcontent.css" rel="stylesheet" type="text/css" />
	<script src="<?php bloginfo('template_url');?>/admins/js/tabcontent.js" 			type="text/javascript" ></script>  
			
	</head>	
	<body>			
			<div id="my_theme_options_content" style="width: 98%;">
                <!-- Tab choosen -->
                	<!-- 
		           <ul id="countrytabs_admin" class="shadetabs">       
		           	 	           
                        <li><a href="#" rel="mySetting_company_logo">Tải Logo</a></li>
                        <li><a href="#" rel="mySetting_area_location">Quản lý các danh mục tùy biến </a></li>
                        <li><a href="#" rel="mySetting_company_image_support">Đổi hình support sidebar</a></li>                   
                    </ul>
                    -->
                 <!-- End Tab choosen -->   
                    <div class="box-tabs">                    
                    <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
                    <?php 
                    //print_r($_POST);
                    global $wpdb;
                    if(wp_verify_nonce($_POST['act_sendmail_nhantin'],'act_sendmail_nhantin') && isset($_POST['update_sendmail']) && $_POST['update_sendmail']=='Send email')
                    {
                    	if($_POST['list_emails'] && $_POST['listbox'])
                    	{
                    		$post_id = $_POST['listbox'];
                    		$emails = $_POST['list_emails'];
                    	
                    	$args = array(
							'post_type'	=> 'post',
							'include'   => array($post_id),
							'posts_per_page' =>1
						);
						query_posts($args);
						
						if(have_posts())
						{
							the_post();
						}
                    								
							$thumbnail = db_get_post_meta($post_id, $wpdb->prefix.'thumbnail');
												
						    $subject=get_the_title($post_id);
						    $home_title = $subject;
						    $home_excerpt = db_shortDesc(get_the_content(), 300);
						    include TEMPLATEPATH.'/mails/xtemplate-email-guitin.php';
							if($emails)
							{
								foreach ($emails as $value) 
								{
										$customer_info = array(
											'name'		=> '',
											'phone'		=> '',
											'email'		=> $value,
											'title'		=> $home_title,
											'message'	=> $home_excerpt,
											'thumbnail' => $thumbnail,
											'linkdetail' => get_permalink($post_id)
				 						);
																			
										## gui mail khach hang
										$result = gui_noi_dung_dang_ky_nhan_tin($config_info,$customer_info,$send_info,0);
										## gui mail admin
										//$result2 = gui_noi_dung_dang_ky_nhan_tin($config_info,$customer_info,$send_info,1);
																			
								}
							}	
							$res = "Gửi email thành công";						
                    	}
                    	else 
                    	{
                    		$res = "Bạn cần chọn Email và tin cần để gửi";
                    	}
                    }
                                        
                    global $wpdb;
                    
                    $selected = '';
                    if($_POST['select_hang_muc']=='-100')
                    {
                    	$selected = 'selected="selected"';
                    }
                    else if($_POST['select_hang_muc']=='-1' || !isset($_POST['select_hang_muc']))
                    {
                    	$selected = '';
                    }
                  
                    //$listuser = get_users(array('meta_key'=> $wpdb->prefix.'user_meta_role','meta_value'=>'dang-ky-nhan-tin'));

                    if($_POST['select_type']!='')
                    {
	                    $num_post		= -1; 
	                    $post_type		= 'post';
	                    $taxonomy		= 'category'; 
	                    $field			= 'slug';
	                    $term			= $_POST['select_type'];	
	                    $metakey		= $wpdb->prefix.'display'; 
	                    $metavalue		= 'display';
	                    
	                    $list_post = db_query_posts_in_terms_child_nopage($num_post, $post_type, $taxonomy, $field, $term, $metakey, $metavalue);
                    }
      		        else 
            			$list_post = db_query_all_post_in_post_type(-1, 'post', 'date', 'DESC', $wpdb->prefix.'display', 'display');        
                    if($_POST['select_hang_muc']!='-1' && $_POST['select_hang_muc']!='-100')
                    {
                    	$hangmuc = $_POST['select_hang_muc'];
                    	$args = array(
							    'meta_query' => array(
                    				'relation' => 'OR',
                    	
							        array(
							            'key' =>  $wpdb->prefix.'user_meta_role',
							            'value' => 'dang-ky-nhan-tin',
							            'compare' => '='
							        ),
							        array(
							            'key' =>  $wpdb->prefix.'dkcb'.$hangmuc,
							            'value' => 'display',
							            'compare' => '='
							        ),
							        array(
							            'key' =>  $wpdb->prefix.'dkct'.$hangmuc,
							            'value' => 'display',
							            'compare' => '='
							        )
							    )
							);
							                    	
                    	//$listuser = get_users(array('meta_key'=> $wpdb->prefix.'user_meta_role','meta_value'=>'dang-ky-nhan-tin','meta_key' => $wpdb->prefix.'dkct'.$hangmuc,'meta_key' =>$wpdb->prefix.'dkcb'.$hangmuc));
                    	$listuser = get_users($args);
                    	//print_r($listuser);
                    }
                    else 
                    	$listuser = get_users(array('meta_key'=> $wpdb->prefix.'user_meta_role','meta_value'=>'dang-ky-nhan-tin'));
                    
                    //$list_post = db_query_all_post_in_post_type(-1, 'post', 'date', 'DESC', $wpdb->prefix.'display', 'display');
                    
                    query_posts($list_post);
                    
                    
                    ?>
                    <form action="" method="post">
                    <ul>
			   			<li style="width: 38%; margin-right: 30px; float: left;">    			
			   			<table>
			   				<tr>
			   					<td style="font-size: 16px;"><b>Danh sách email </b></td>
			   					<td style="font-size: 16px;"><b>Chọn email</b></td>
			   				</tr>
			   				
			   				<tr>
			   					<td>
			   						<select name="list_emails[]" multiple="multiple" size="22" >
						   				<?php 
						   				if($listuser)
						   				{
							   				foreach ($listuser as $key=>$value) 
							   				{
							   				?>
								   				<option <?php echo $selected;?> value="<?php echo $value->user_email;?>"><?php echo $value->user_email;?></option>	
							   				<?php 
							   				
							   				}
						   				}
						   				?>
			   						</select>
			   					</td>
			   					<td valign="top">
			   						 <?php 
		                            $args = null;
									$args = array('title_li'  => '','orderby' => 'id','taxonomy' => 'hang-muc','hide_empty' => 0,'hierarchical'  => true,'depth'  => 1);
									$hangmuc_list = get_categories($args);
		                            ?>
			   						<select name="select_hang_muc" onchange="this.form.submit()">
			   							<option <?php if(isset($_POST['select_hang_muc']) && $_POST['select_hang_muc']=='-1') echo 'selected="selected"';?> value="-1">Chọn từng email</option>
			   							
			   							<option <?php if(isset($_POST['select_hang_muc']) && $_POST['select_hang_muc']=='-100') echo 'selected="selected"';?> value="-100">Chọn Tất cả</option>
			   						</select>
			   						
			   					</td>
			   				</tr>
			   			</table>
			   			</li>
			   			
			   			<!-- - -->
			   			<li style="width: 38%; float: left;">    			
			   			<table >
			   				<tr>
			   					<td style="font-size: 16px;"><b>Chọn tin cần gửi</b></td>
			   					<td style="font-size: 16px;"><b>Gửi email</b></td>
			   				</tr>
			   				<tr>
			   					<td>
			   						<select name="listbox" size="3" style="height: 400px;"  >
						   			<?php 
						   				if(have_posts())
						   				{
							   				while (have_posts())							   				 
							   				{
							   					the_post();
							   				?>
								   				<option value="<?php the_ID();?>"><?php the_title();?></option>	
							   				<?php 
							   				
							   				}
						   				}
						   				?>
			   						</select>
			   						<!-- 
			   						<br/>
			   						Tin khác: vd: http:google.com <br/>
			   						<input type="text" name="listbox_tinkhac" size="60" value=""/>
			   						-->
			   					</td>
			   					<td>
			   						<input type="submit" name="update_sendmail" id="update_sendmail" value="Send email">
			   					</td>
			   				</tr>
			   			</table>
			   			</li> 
			   			 			
			   		</ul>
			   		<?php wp_nonce_field('act_sendmail_nhantin','act_sendmail_nhantin');?>
                    </form>
                    <?php 
                    if($res!='')
                    echo '<script> alert("'.$res.'"); </script>';
                    ?>	 
                     <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->	                    
	           		 </div>
		             <script type="text/javascript">
						var countries=new ddtabcontent("countrytabs_admin")
						countries.setpersist(true)
						countries.setselectedClassTarget("link") 
						countries.init()
					</script>						             
    	</div>
    	</body>
	</html>